package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.utils.HttpUtils;
import com.ruoyi.common.utils.MoneyUtil;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Service("yunyinqianPayService")
public class YunYinQianPayServiceImpl extends BasePayment {




    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String qrcode = userRecipientInfo.getReceiptQrcode();
//        String qrcode_ = HttpUtils.getParam(qrcode,"userAppId");
        String accountJson = userRecipientInfo.getAccountJson();
        JSONObject  accountObj = JSONObject.parseObject(accountJson);
        String token = accountObj.getString("ck");
        headerMap.put("Host","yhyser.yunhuiyypay.com");
        headerMap.put("User-Agent","Mozilla/5.0 (iPad; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");
        JSONObject  reqOrderObj  = new JSONObject();
        JSONObject reqDataObj = new JSONObject();
        dataMap.put("ifCode","alipay");
        dataMap.put("amount", MoneyUtil.fenToYuan(orderInfo.getMoney()+""));
        dataMap.put("mchNo",userRecipientInfo.getChannelAccount());
        dataMap.put("userId",accountObj.getString("userId"));
        dataMap.put("orderSubject",accountObj.getString("orderSubject"));
        StringBuilder buf = new StringBuilder("");
        HttpUtils.buildPayParams(buf, dataMap);
        String preStr = buf.toString();
        logger.info("云银签请求："+preStr);

        String submitPayRes = HttpUtils.sendPostRequest("https://yhyser.yunhuiyypay.com/api/anon/infoPayOrder",preStr,headerMap);
        JSONObject  submitPayResObj = JSONObject.parseObject(submitPayRes);
        if(submitPayResObj.getIntValue("code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg",submitPayResObj.getString("msg"));
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientInfo.setDisabledInfo(submitPayResObj.getString("retMsg"));
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        logger.info("云银签返回:{}",submitPayResObj.toJSONString());
        JSONObject payDataObj = submitPayResObj.getJSONObject("data");
        JSONObject originalResponse = payDataObj.getJSONObject("originalResponse");
        /**
         * var orderPriceCorrected = response.orderPrice / 100;
         * 				var query = encodeURIComponent('?rechargeAccount=' + response.rechargeAccount + '&avatarUrl=' + 'https://yunyinsign-szpay-test.oss-cn-guangzhou.aliyuncs.com/avatar/zlogo.jpg' + '&orderNumber=' + response.orderNumber + '&orderPrice=' + orderPriceCorrected + '&mchAppId=' + response.mchAppId);
         * 				window.location.href = 'https://ds.alipay.com/?scheme=' + encodeURIComponent('alipays://platformapi/startapp?appId=' + response.zfbAppId + '&page=pages/index/index' + query);
         */
        int orderPriceCorrected = originalResponse.getIntValue("orderPrice")/100;
        String payUrl = "alipays://platformapi/startapp?appId=" + originalResponse.getString("zfbAppId") + "&page=pages/index/index" +URLEncoder.encode("?rechargeAccount=" + originalResponse.getString("rechargeAccount") + "&avatarUrl=" + originalResponse.getString("avatarUrl") + "&orderNumber=" + originalResponse.getBigInteger("orderNumber") + "&orderPrice=" + orderPriceCorrected + "&mchAppId=" + originalResponse.getString("mchAppId"));

        jsonObject.put("payUrl",payUrl);
        jsonObject.put("orderNo",originalResponse.getBigInteger("orderNumber") );
        jsonObject.put("code","200");
        return jsonObject;
    }


    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws Exception {
        return createPayOrder(orderInfo);
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }
}
